// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_d3d9_render_target.h"
#include "mk_d3d9_render_device.h"
#include "mk_d3d9_texture.h"
#include "mk_error.h"
#include "mk_engine.h"
#include "mk_render_device.h"
#include "mk_resource_manager.h"

namespace MK3DE
{
	CD3D9RenderTarget::CD3D9RenderTarget()
	{
		_renderTargetTexture = NULL;
		_lpColorBuffer = NULL;
		_lpDSBuffer = NULL;
	}

	CD3D9RenderTarget::~CD3D9RenderTarget()
	{
		SAFE_RELEASE(_lpDSBuffer);
		SAFE_RELEASE(_lpColorBuffer);
		SAFE_RELEASE(_renderTargetTexture);
	}

	BOOL CD3D9RenderTarget::_Create(DWORD width, DWORD height, PIXEL_FORMAT format, BOOL bDSBuffer)
	{
		CEngine* engine = GetEngine();
		CD3D9RenderDevice* renderDevice = (CD3D9RenderDevice*)engine->GetRenderDevice();
		CResourceManager* resourceManager = engine->GetResourceManager();
		LPDIRECT3DDEVICE9 pd3d9Device = renderDevice->GetD3DDevice();
		const D3DPRESENT_PARAMETERS& d3dpp = renderDevice->GetD3DPresentParameters();
		BOOL ret = FALSE;
		BOOL hr = E_FAIL;

		ret = resourceManager->GetTexture2D(width, height, format, 1, TU_RENDER_TARGET, _renderTargetTexture);
		MK_PROCESS_ERROR(ret);

		if (bDSBuffer && renderDevice->IsStencilBuffer())
		{
			hr = pd3d9Device->CreateDepthStencilSurface(width, height, d3dpp.AutoDepthStencilFormat, 
				D3DMULTISAMPLE_NONE, 0, TRUE, &_lpDSBuffer, NULL);
			MK_PROCESS_COM_ERROR(hr);
		}

		ret = ((CD3D9Texture2D*)_renderTargetTexture)->GetRenderTargetSurface(_lpColorBuffer);
		MK_PROCESS_ERROR(ret);

		ret = TRUE;

Exit0:
		if (!ret)
		{
			SAFE_RELEASE(_lpDSBuffer);
			SAFE_RELEASE(_lpColorBuffer);
			SAFE_RELEASE(_renderTargetTexture);
		}

		return ret;
	}

	BOOL CD3D9RenderTarget::_SetRenderTarget(LPDIRECT3DDEVICE9 pDevice)
	{
		HRESULT hr = E_FAIL;
		hr = pDevice->SetRenderTarget(0, _lpColorBuffer);
		MK_PROCESS_COM_ERROR(hr);

		if (_lpDSBuffer)
		{
			hr = pDevice->SetDepthStencilSurface(_lpDSBuffer);
			MK_PROCESS_COM_ERROR(hr);
		}

		hr = S_OK;
Exit0:
		return hr == S_OK ? TRUE : FALSE;
	}

	BOOL CD3D9RenderTarget::HasIndependantDSBuffer()
	{
		return _lpDSBuffer ? TRUE : FALSE;
	}
}